package com.swt.gap.service.impl;

import com.swt.gap.dao.OntologyRelationshipMapper;
import com.swt.gap.dao.OntologyRelationshipPropertyMapper;
import com.swt.gap.dao.TaskRelationshipTypeDao;
import com.swt.gap.model.entity.OntologyRelationship;
import com.swt.gap.model.entity.TaskRelationshipType;
import com.swt.gap.model.vo.CountPropertyVO;
import com.swt.gap.model.vo.RelationshipAndPropertyVO;
import com.swt.gap.model.vo.RelationshipCountPropertyVO;
import com.swt.gap.service.OntologyRelationshipService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 请填写说明
 *
 * @author sx
 * @date 2020/6/19 8:44
 */
@Service
public class OntologyRelationshipServiceImpl implements OntologyRelationshipService {

    private final TaskRelationshipTypeDao taskRelationshipTypeDao;
    private final OntologyRelationshipMapper ontologyRelationshipMapper;
    private final OntologyRelationshipPropertyMapper ontologyRelationshipPropertyMapper;

    public OntologyRelationshipServiceImpl(TaskRelationshipTypeDao taskRelationshipTypeDao,
                                           OntologyRelationshipMapper ontologyRelationshipMapper,
                                           OntologyRelationshipPropertyMapper ontologyRelationshipPropertyMapper) {
        this.taskRelationshipTypeDao = taskRelationshipTypeDao;
        this.ontologyRelationshipMapper = ontologyRelationshipMapper;
        this.ontologyRelationshipPropertyMapper = ontologyRelationshipPropertyMapper;
    }
    @Override
    public List<RelationshipCountPropertyVO> listCountPropertyByTaskId(Integer taskId) {
        List<RelationshipCountPropertyVO> retValue = new ArrayList<>();
        List<TaskRelationshipType> taskRelationshipTypes = taskRelationshipTypeDao.listTaskRelationshipTypeByTaskId(taskId);
        taskRelationshipTypes.forEach(taskRelationshipType -> {
            RelationshipCountPropertyVO relationshipCountProperty = new RelationshipCountPropertyVO();
            relationshipCountProperty.setRelationshipType(taskRelationshipType.getRelationshipType());
            if (null != taskRelationshipType.getOntologyRelationshipId()) {
                relationshipCountProperty.setOntologyRelationshipId(taskRelationshipType.getOntologyRelationshipId());
                relationshipCountProperty.setCountProperties(ontologyRelationshipPropertyMapper
                        .listCountPropertiesByRelationshipId(taskRelationshipType.getOntologyRelationshipId()));
            }
            retValue.add(relationshipCountProperty);
        });
        return retValue;
    }

    @Override
    public List<RelationshipAndPropertyVO> listRelationshipsByStartIdAndEndId(Integer startNodeLabelId,
                                                                             Integer endNodeLabelId) {
        List<RelationshipAndPropertyVO> retValue  = ontologyRelationshipMapper
                .listRelationshipsByStartIdAndEndId(startNodeLabelId, endNodeLabelId);
        retValue.forEach(rel -> rel.setRelationshipProperties(ontologyRelationshipPropertyMapper
                .selectByRelationshipId(rel.getRelationshipId())));
        return retValue;
    }
}
